package com.twu.biblioteca;

import com.twu.biblioteca.command.*;
import com.twu.biblioteca.domain.Library;
import com.twu.biblioteca.model.Book;
import com.twu.biblioteca.model.Customer;
import com.twu.biblioteca.model.Movie;
import com.twu.biblioteca.security.SecuritySupport;

import java.util.ArrayList;
import java.util.List;

import static com.twu.biblioteca.engine.Engine.start;
import static com.twu.biblioteca.util.ConsoleUtil.println;

public class BibliotecaApp {

    private static final String WELCOME_MESSAGE = "Welcome To The Bangalore Public Library!";

    public static void main(String[] args) {
        println(WELCOME_MESSAGE);

        Library library = initLibrary();
        CommandFactory commandFactory = new CommandFactory(library);
        listAllBooks(commandFactory);
        start(commandFactory, initSecurity(library));
    }

    private static void listAllBooks(CommandFactory commandFactory) {
        println(commandFactory.getCommand("List Books").execute());
    }

    private static SecuritySupport initSecurity(Library library) {
        SecuritySupport securitySupport = new SecuritySupport();
        securitySupport.setLibrary(library);
        securitySupport.setSecurityRequiredCommands(getSecurityRequiredCommands());
        return securitySupport;
    }

    private static List<Class> getSecurityRequiredCommands() {
        List<Class> securityRequiredCommands = new ArrayList<Class>();
        securityRequiredCommands.add(CheckoutBookCommand.class);
        securityRequiredCommands.add(CheckoutMovieCommand.class);
        securityRequiredCommands.add(ReturnBookCommand.class);
        securityRequiredCommands.add(ReturnMovieCommand.class);
        return securityRequiredCommands;
    }

    private static Library initLibrary() {
        Library library = new Library();
        initBooks(library);
        initMovies(library);
        initCustomers(library);
        return library;
    }

    private static void initCustomers(Library library) {
        library.addCustomer(new Customer("111-2222", "Tom", "four corner", "hi.@163.com", "1345678", "customer1"));
        library.addCustomer(new Customer("111-2223", "Jerry", "three corner", "qq.@163.com", "999999", "customer2"));
    }

    private static void initMovies(Library library) {
        library.addMovie(new Movie("M00001", "Transcendence", "2014", "Wally Pfister", 6));
        library.addMovie(new Movie("M00002", "The Hobbit: An Unexpected Journey", "2012", "Peter Jackson", 8));
        library.addMovie(new Movie("M00003", "The Hobbit: The Desolation of Smaug", "2013", "Peter Jackson", 8));
        library.addMovie(new Movie("M00004", "The Avengers", "2012", "Joss Whedon", 8));
    }

    private static void initBooks(Library library) {
        library.addBook(new Book("B11111", "Thinking in Java (4th Edition)", "Bruce Eckel", "February 20, 2006"));
        library.addBook(new Book("B11112", "Head First Java, 2nd Edition", "Kathy Sierra , Bert Bates", "February 9, 2005"));
        library.addBook(new Book("B11113", "JavaScript:The Good Parts", "Douglas Crockford", "May 1, 2008"));
    }
}
